﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.Text;
using MVC.ApiExplorer;
using MVC.ApiExplorer.Demo.Models;

namespace The.controller.you.want.to.explore
{
	public class APIController : Controller
	{
		[NonApi]
		[Description("As it be decorated with NonApi. This action won't be displayed in test page.")]
		public ActionResult Index()
		{
			return View();
		}

		[Description("A normal page I want to be testable.")]
		public ActionResult NormalPage()
		{
			return View();
		}

		[HttpGet]
		[Description("An action which accepts http-get invocation only.")]
		public ActionResult GetOnly(string msg, [Description("This value is a nullable integer.")]int? value)
		{
			return Json(new { msg = msg, value = value }, JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		[Description("An action which accepts http-post invocation only.")]
		public ActionResult PostOnly(string msg, string value1, int value2, bool valut3)
		{
			return Json(new { msg = msg, value1 = value1, value2 = value2, value3 = valut3 });
		}

		[HttpPost]
		[ValidateAntiForgeryToken(Salt="SaltValue")]
		[Description("An action which accepts http-post invocation only and validates an anit-forgery toekn with a salt value.")]
		public ActionResult PostOnlyAndValidWithSalt(string msg, string value)
		{
			return Json(new { msg = msg, value = value });
		}

		[HttpPut]
		[ValidateAntiForgeryToken]
		[Description("An action which accepts http-put invocation only.")]
		public ActionResult PutOnly(string msg, string value)
		{
			return Json(new { msg = msg, value = value });
		}

		[HttpDelete]
		[ValidateAntiForgeryToken]
		[Description("An action which accepts http-delete invocation only.")]
		public ActionResult DeleteOnly(string msg, string value)
		{
			return Json(new { msg = msg, value = value });
		}

		[Description("Get the javascript snippet.")]
		public ActionResult GetJavascript(string msg)
		{
			return JavaScript(string.Format("//this is a javascript code.\r\nalert('{0}');", msg));
		}

		[HttpPost]
		[Description("Upload a file to service.")]
		public ActionResult UploadFile(HttpPostedFileBase file)
		{
			return Json(new { Message = "Received a file from client.", FileName = file.FileName });
		}

		[HttpPost]
		[Description("Download a file form service.")]
		public ActionResult DownloadFile()
		{
			var data = Encoding.UTF8.GetBytes("Dummy content.");
			return File(data, "application/zip", "DummyFile.txt");
		}

		[HttpPost]
		[ActionName("changed-action-name")]
		[Description("An action which the name was changed by decorating with an ActionName attritube.")]
		public ActionResult OriginalActionName(string msg, string value1, int value2 = 0, bool valut3 = false)
		{
			return Json(new { msg = msg, value1 = value1, value2 = value2, value3 = valut3 });
		}

	}
}
